package com.htgd.gardenexpo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dao.GeBaseDictItemDao;
import com.htgd.gardenexpo.entity.GeBaseDict;
import com.htgd.gardenexpo.dao.GeBaseDictDao;
import com.htgd.gardenexpo.entity.GeBaseDictItem;
import com.htgd.gardenexpo.page.DictQuery;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.vo.LabelValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-07
 */
@Service
public class GeBaseDictServiceImpl extends ServiceImpl<GeBaseDictDao, GeBaseDict> implements GeBaseDictService {

    @Autowired
    private GeBaseDictDao geBaseDictDao;

    @Autowired
    private GeBaseDictItemDao geBaseDictItemDao;

    @Override
    public boolean codeExist(String code) {
        return lambdaQuery().eq(GeBaseDict::getDictCode, code)
                .eq(GeBaseDict::getDelFlag,0)
                .exists();
    }

    @Override
    public String getStatusText(String value) {
        String result = "";
        GeBaseDict geBaseDict = geBaseDictDao.getByCode("status");
        QueryWrapper<GeBaseDictItem> geBaseDictItemQueryWrapper = new QueryWrapper<>();
        geBaseDictItemQueryWrapper.eq("dict_id",geBaseDict.getId());
        List<GeBaseDictItem> list1 = geBaseDictItemDao.selectList(geBaseDictItemQueryWrapper);
        for (GeBaseDictItem geBaseDictItem : list1) {
            if(value.equals(geBaseDictItem.getItemValue())){
                result = geBaseDictItem.getItemText();
                break;
            }
        }
        return result;
    }

    @Override
    public List<LabelValue> getItemByCode(String code) {
        List<LabelValue> list = new ArrayList<>();
        GeBaseDict geBaseDict = geBaseDictDao.getByCode(code);
        QueryWrapper<GeBaseDictItem> geBaseDictItemQueryWrapper = new QueryWrapper<>();
        geBaseDictItemQueryWrapper.eq("dict_id",geBaseDict.getId())
                .orderByAsc("sort_order");
        List<GeBaseDictItem> list1 = geBaseDictItemDao.selectList(geBaseDictItemQueryWrapper);
        for (GeBaseDictItem geBaseDictItem : list1) {
            LabelValue levelValue = new LabelValue();
            levelValue.setLabel(geBaseDictItem.getItemText());
            levelValue.setValue(geBaseDictItem.getItemValue());
            list.add(levelValue);
        }
        return list;
    }

    @Override
    public String getTopText(String value) {
        String result = "";
        GeBaseDict geBaseDict = geBaseDictDao.getByCode("top");
        QueryWrapper<GeBaseDictItem> geBaseDictItemQueryWrapper = new QueryWrapper<>();
        geBaseDictItemQueryWrapper.eq("dict_id",geBaseDict.getId());
        List<GeBaseDictItem> list1 = geBaseDictItemDao.selectList(geBaseDictItemQueryWrapper);
        for (GeBaseDictItem geBaseDictItem : list1) {
            if(value.equals(geBaseDictItem.getItemValue())){
                result = geBaseDictItem.getItemText();
                break;
            }
        }
        return result;
    }

    @Override
    public String getTextByCodeAndValue(String code, String value) {
        return geBaseDictDao.getTextByCodeAndValue(code,value);
    }

    @Override
    public String getFeedbackTypeText(String value) {
        String result = "";
        GeBaseDict geBaseDict = geBaseDictDao.getByCode("feedback_type");
        QueryWrapper<GeBaseDictItem> geBaseDictItemQueryWrapper = new QueryWrapper<>();
        geBaseDictItemQueryWrapper.eq("dict_id",geBaseDict.getId());
        List<GeBaseDictItem> list1 = geBaseDictItemDao.selectList(geBaseDictItemQueryWrapper);
        for (GeBaseDictItem geBaseDictItem : list1) {
            if(geBaseDictItem.getItemValue() != null && value.equals(geBaseDictItem.getItemValue())){
                result = geBaseDictItem.getItemText();
                break;
            }
        }
        return result;
    }

    @Override
    public String getByCode(String code) {
        GeBaseDict geBaseDict = geBaseDictDao.getByCode(code);
        if(geBaseDict==null){
            return "";
        }
        return geBaseDict.getId();
    }

    @Override
    public String getRecommendTypeText(String value) {
        String result = "";
        GeBaseDict geBaseDict = geBaseDictDao.getByCode("recommend_type");
        QueryWrapper<GeBaseDictItem> geBaseDictItemQueryWrapper = new QueryWrapper<>();
        geBaseDictItemQueryWrapper.eq("dict_id",geBaseDict.getId());
        List<GeBaseDictItem> list1 = geBaseDictItemDao.selectList(geBaseDictItemQueryWrapper);
        for (GeBaseDictItem geBaseDictItem : list1) {
            if(value==null){
                break;
            }
            if(geBaseDictItem.getItemValue() != null && value.equals(geBaseDictItem.getItemValue())){
                result = geBaseDictItem.getItemText();
                break;
            }
        }
        return result;
    }

    @Override
    public PageInfo<GeBaseDict> pageList(DictQuery dictQuery) {
        PageHelper.startPage(dictQuery.getPageNum(),dictQuery.getPageSize());
        List<GeBaseDict> list = geBaseDictDao.pageList(dictQuery);
        return new PageInfo<>(list);
    }

    @Override
    public String getGoodsType(String value) {
        String result = "";
        GeBaseDict geBaseDict = geBaseDictDao.getByCode("goods_type");
        QueryWrapper<GeBaseDictItem> geBaseDictItemQueryWrapper = new QueryWrapper<>();
        geBaseDictItemQueryWrapper.eq("dict_id",geBaseDict.getId());
        List<GeBaseDictItem> list1 = geBaseDictItemDao.selectList(geBaseDictItemQueryWrapper);
        for (GeBaseDictItem geBaseDictItem : list1) {
            if(value==null){
                break;
            }
            if(geBaseDictItem.getItemValue() != null && value.equals(geBaseDictItem.getItemValue())){
                result = geBaseDictItem.getItemText();
                break;
            }
        }
        return result;
    }
}
